<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/20/020
 * Time: 9:43
 */

namespace app\index\controller;


use app\Lib\Api\User\addWeiXinUser;
use app\Lib\Api\User\exitWeiXinUser;
use app\Lib\Exception\BlException;
use app\Lib\Model\Login\DoLogin;
use app\Lib\Model\Login\WeiXiGetOpenId;
use app\Lib\TopClient;
use think\Controller;
use think\Cookie;
use think\Url;

class WeiXinOpenIdLogin extends Controller
{
    public function index(){
        $url =  'http://' . $_SERVER ['HTTP_HOST'] .'/'.Cookie::get('firstUrl');
        header('Location:'.$url);
    }
    public function weiXinLogin(){
        try{
            if (! isset ( $_GET ['code'] )) {
                throw new BlException('参数错误');
            }
            $model =  (new WeiXiGetOpenId());
            $model->setCode($_GET ['code']);
            $openId = $model->getOpenId();
            //检验用户是否存在
            $exitWeiXin = new exitWeiXinUser();
            $exitWeiXin->setArgs('openId',$openId);
            $result = (new TopClient($exitWeiXin))->getSuccessData();
//        $result = false;
            if(!$result){
                //不存在添加用户
                $url =  'http://' . $_SERVER ['HTTP_HOST'] .'/Index/WeiXinOpenIdLogin/addWeiXinLogin/openId/'.$openId;
                echo  $url;
                header('Location:'.$url);
                exit();
            }else{
                //存在直接登录
                $loginModel = (new DoLogin());
                $loginModel->setOpenId($openId);
                $loginModel->setLoginKey('weiXin_web_log_key');
                $loginModel->weiXinLogin();
                header('Location:'.Cookie::get('firstUrl'));
            }
        }catch (\Exception $e){
            echo $e->getMessage();
        }

        //跳转页面


    }

    public function addWeiXinLogin(){
        try{
            if($this->request->isPost()){
                $model = new addWeiXinUser();
                $post = input('post.');
                $model->setArgs('openId',$post['openId']);
                $model->setArgs('userName',$post['userName']);
                $model->setArgs('tel',$post['tel']);
                $model->setArgs('sex',$post['sex']);
                $model->setArgs('birthday',$post['birthday']);
                (new TopClient($model))->getSuccessData();
                $loginModel = (new DoLogin());
                $loginModel->setOpenId($post['openId']);
                $loginModel->setLoginKey('weiXin_web_log_key');
                $loginModel->weiXinLogin();
                return ['code'=>1,'msg'=>'完善成功，请稍等','url'=>'http://' . $_SERVER ['HTTP_HOST'] .'/'.Cookie::get('firstUrl')];
            }else{
//                echo  'http://'.$_SERVER ['HTTP_HOST'].'/'.Cookie::get('firstUrl');
                $openId = input('openId','111');
                $this->assign('openId',$openId);
                return $this->fetch();
            }
        }catch (\Exception $e){
            return ['code'=>-1,'msg'=>$e->getMessage()];
        }
    }
}